package com.liit.controller.weixin.process;

import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import cn.liit.weixin.core.basic.accesstoken.AccessTokenSupport;
import cn.liit.weixin.core.config.Config;
import cn.liit.weixin.message.model.req.ReqBaseMessage;
import cn.liit.weixin.message.model.req.ScanSubscribeEventMessage;
import cn.liit.weixin.message.model.req.SubscribeUnsubscribeEventMessage;
import cn.liit.weixin.message.model.resp.RespBaseMessage;
import cn.liit.weixin.message.model.resp.TextMessage;
import cn.liit.weixin.message.process.receive.ReceiveProcessor;
import cn.liit.weixin.userinfo.model.UserInfo;
import cn.liit.weixin.userinfo.utils.Util;

import com.liit.entity.MeetingFormMap;
import com.liit.entity.MeetingWxuserFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.mapper.WeixinUserMapper;
import com.liit.spring.SpringUtils;

/**
 * 关注消息处理器
 * 
 * @author zhangboqi
 *
 */
public class SubscribeMsgProcess extends ReceiveProcessor {

	public SubscribeMsgProcess(String msgType, String event) {
		super(msgType, event);
	}

	@Override
	public RespBaseMessage doProcess(ReqBaseMessage reqBaseMessage) {
		RespBaseMessage respBaseMessage = null;
		try {
			String openid = reqBaseMessage.getFromUserName();
			UserInfo userInfo = Util.getUserInfo(AccessTokenSupport.getAccess_token(), openid);

			WeixinUserMapper mapper = SpringUtils.getBean("weixinUserMapper");
			WeixinUserFormMap weixinUserFormMap = mapper.findbyFrist("openid", openid, WeixinUserFormMap.class);
			if (weixinUserFormMap == null) {
				weixinUserFormMap = new WeixinUserFormMap();
				weixinUserFormMap.put("openid", openid);
				weixinUserFormMap.put("provinceName", userInfo.getProvince());
				weixinUserFormMap.put("cityName", userInfo.getCity());
				weixinUserFormMap.put("headerUrl", userInfo.getHeadimgurl());
				weixinUserFormMap.put("name", userInfo.getNickname());
				weixinUserFormMap.put("doctorSex", userInfo.getSex());
				weixinUserFormMap.put("focusTime", new Timestamp(userInfo.getSubscribe_time() * 1000));
				weixinUserFormMap.put("isReg", 0);
				mapper.addEntity(weixinUserFormMap);
			} else {
				weixinUserFormMap.put("name", userInfo.getNickname());
				weixinUserFormMap.put("headerUrl", userInfo.getHeadimgurl());
				weixinUserFormMap.put("doctorSex", userInfo.getSex());
				mapper.editEntity(weixinUserFormMap);
			}

			// 带参数扫码关注
			if (reqBaseMessage instanceof ScanSubscribeEventMessage) {
				ScanSubscribeEventMessage req = (ScanSubscribeEventMessage) reqBaseMessage;
				// eventKey以qrscene_开头
				if (StringUtils.isBlank(req.getEventKey())) {
					throw new NullPointerException("eventKey is null!");
				}
				String meetingId = req.getEventKey().substring(8);
				if (weixinUserFormMap.getInt("isReg") == 0) {
					if (StringUtils.isNotEmpty(req.getEventKey())) {
						respBaseMessage = new TextMessage("<a href='" + Config.notifyurl + "/weixin/reg.shtml?openid=" + openid + "'>您还没有注册，请点击注册</a>");
					}
				} else {
					MeetingFormMap meetingFormMap = mapper.findbyFrist("id", meetingId, MeetingFormMap.class);
					// 会议关闭
					if (meetingFormMap.getInt("status") == 1) {
						respBaseMessage = new TextMessage("您好!会议【" + meetingFormMap.getStr("name") + "】已关闭");
					} else {
						MeetingWxuserFormMap meetingWxuserFormMap = new MeetingWxuserFormMap();
						meetingWxuserFormMap.put("openid", openid);
						meetingWxuserFormMap.put("meetingId", meetingId);
						List<MeetingWxuserFormMap> list = mapper.findByNames(meetingWxuserFormMap);
						if (CollectionUtils.isEmpty(list)) {
							MeetingWxuserFormMap record = new MeetingWxuserFormMap();
							record.put("meetingId", meetingId);
							record.put("openid", openid);
							mapper.addEntity(record);
							respBaseMessage = new TextMessage("您好!欢迎加入【" + meetingFormMap.getStr("name") + "】");
						} else {
							respBaseMessage = new TextMessage("您好!您已加入【" + meetingFormMap.getStr("name") + "】");
						}
					}
				}
			} else if (reqBaseMessage instanceof SubscribeUnsubscribeEventMessage) {// 普通关注
				respBaseMessage = new TextMessage("您好! 欢迎您关注康维信公众号");
			}
		} catch (Exception e) {
			respBaseMessage = new TextMessage("您好! 欢迎您关注康维信公众号");
			e.printStackTrace();
		}
		return respBaseMessage;
	}
}
